package com.it2297.view;

import java.awt.BorderLayout;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

import com.it2297.component.AbstractPagination;
import com.it2297.controller.ProductController;
import com.it2297.main.UserApp;
import com.it2297.model.Product;
import com.it2297.renderer.commentCellRenderer;

public class ProductCommentView extends JPanel {
	private static String ProductName;
	private final ProductController controller = new ProductController(
			ProductCommentView.this);
	private final JTable table = new JTable();
	private final DefaultTableModel tableModel = new DefaultTableModel(0, 2) {
		@Override
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	};
	private final commentCellRenderer tableCellRenderer = new commentCellRenderer();
	private final AbstractPagination pagination = new ViewPagination();
	private final JToolBar toolBar = new JToolBar();
	private final Product product = new Product();
	private final JButton button_1 = new JButton("Post");
	private final JButton button_2 = new JButton("Remove");
	private final JButton btnrefresh = new JButton("Refresh");

	public ProductCommentView() {
		initGUI();
	}

	private void initGUI() {
		pagination.refresh();
		this.setBounds(100, 100, 750, 570);
		this.setLayout(new BorderLayout(0, 0));
		this.add(table, BorderLayout.CENTER);
		this.add(pagination, BorderLayout.SOUTH);

		table.setRowHeight(100);
		table.setShowGrid(false);
		table.setFocusable(false);
		table.setModel(tableModel);

		table.getColumnModel().getColumn(0).setMaxWidth(100);
		table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(1).setCellRenderer(tableCellRenderer);

		toolBar.setFloatable(false);

		add(toolBar, BorderLayout.NORTH);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				controller.showCreateComment(getProductName());
				pagination.setTotal(controller.getRecordNumComment());
				pagination.refresh();
			}
		});
		button_1.setIcon(new ImageIcon(NewShopView.class
				.getResource("/com/alee/extended/filechooser/icons/edit.png")));
		button_1.setFocusable(false);
		btnrefresh.setIcon(new ImageIcon(NewShopView.class
				.getResource("/com/alee/extended/filechooser/icons/edit.png")));
		btnrefresh.setFocusable(false);
		btnrefresh.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				pagination.refresh();
			}
		
				
		});
		
		
		toolBar.add(btnrefresh);
		toolBar.add(button_1);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean response = controller.deleteComment();
				if (response) {
					controller.deleteComment();
					pagination.setTotal(controller.getRecordNumComment());
					pagination.refresh();
				}

			}
		});
		button_2.setIcon(new ImageIcon(ProductCommentView.class
				.getResource("/com/alee/extended/filechooser/icons/cancel.png")));
		button_2.setFocusable(false);
		if(UserApp.getCurrentUser().getRole().equals("ADMIN")){
		toolBar.add(button_2);
		}

	}

	public Object getSelectedItem() {
		return table.getValueAt(table.getSelectedRow(),
				table.getSelectedColumn());
	}

	public Object[] getSelectedItems() {
		ArrayList<Object> items = new ArrayList<Object>();
		for (int i : table.getSelectedRows()) {
			items.add(table.getValueAt(i, table.getSelectedColumn()));
		}
		return items.toArray();
	}

	private class ViewPagination extends AbstractPagination {
		
		protected ViewPagination() {
			this.setCount(3);
			this.setTotal(controller.getRecordNumComment());
		}

		@Override
		protected void action(Object[] data) {
			// clear all row
			tableModel.setRowCount(0);

			// add new row base on data size
			for (int i = 0; i < data.length; i++) {
				tableModel.addRow(new Object[] { data[i], data[i] });
			}
		}

		@Override
		protected Object[] subData(int position, int count) {
			return controller.getMultipleComment(position, count, getProductName()).toArray();
		}
	}

	{

	}

	public void addCommentListener(MouseListener commentCellRenderer) {
		table.addMouseListener(commentCellRenderer);
	}

	public Product getSelected() {
		return (Product) table.getModel().getValueAt(table.getSelectedRow(),
				table.getSelectedColumn());
	}

	public String getProductName() {
		return ProductName  ;
	}

	public void setProductName(String productName) {
		this.ProductName = productName;
	}
	
	public void refresh(){
		pagination.refresh();
	}
	
	
}
